//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
  item:0,
  tab:0,
  playlist:[{
    id:1,
    title:'小永远',
    singer:'何洁',
    src:'https://sharefs.yun.kugou.com/201911141609/5836c907da10a57202fa71d357075bdd/G015/M00/19/03/Tw0DAFUX7wyAJ_T0ADqxFlXj3bE747.mp3',
    coverImgUrl:'https://y.gtimg.cn/music/photo_new/T001R300x300M000001Zu9d92MKxNu.jpg?max_age=2592000'
  },
  {
    id:2,
    title:'光',
    singer:'陈粒',
    src: 'https://sharefs.yun.kugou.com/201911141615/1c88a254af67fa60cf706f63930f739a/G009/M0A/05/0D/qYYBAFUMqtmAVa71ACJ94iUy7yk539.mp3',
    coverImgUrl: 'http://puui.qpic.cn/media_img/0/15194981536286404/0'
  },
  {
    id:3,
    title:'桃子',
    singer:'IU',
    src:'https://sharefs.yun.kugou.com/201911141618/dec88a70d37b132588d735a9b099d197/G003/M0A/01/08/o4YBAFUIbCaAFkenAC7BmKxTRFw960.mp3',
    coverImgUrl: 'https://img03.sogoucdn.com/v2/thumb?created_by=js_web&t=2&url=http%3A%2F%2Fy.gtimg.cn%2Fmusic%2Fphoto_new%2FT001R300x300M000004Bjyj52RTYOj.jpg%3Fmax_age%3D2592000&appid=200580'
  },
  {
    id:4,
    title:'昨日青空',
    singer:'尤长靖',
    src:'https://sharefs.yun.kugou.com/201911141613/4a026e22cc3a3548db9fbbdbcf571d8a/G135/M03/16/17/xw0DAFtQSuGAPq-QAERZUOQLzE8717.mp3',
    coverImgUrl: 'https://img03.sogoucdn.com/v2/thumb?created_by=js_web&t=2&url=http%3A%2F%2Fy.gtimg.cn%2Fmusic%2Fphoto_new%2FT001R300x300M000003imGMJ0gPd00.jpg%3Fmax_age%3D2592000&appid=200580'
  }],
  state:'paused',
   playIndex:0,
    play:{
      currentTime:'00:00',
      duration:'00:00',
      percent:0,
      title:'',
      singer:'',
      coverImgUrl:'/images/img1.jpg',
    }
    },
  audioCtx: null,
  onReady: function () {
    this.audioCtx = wx.createInnerAudioContext()
    this.setMusic(0)
  },
  setMusic: function (index) {
    var music = this.data.playlist[index]
    this.audioCtx.src = music.src
    this.setData({
      playIndex: index,
      'play.title': music.title,
      'play.singer': music.singer,
      'play.coverImgUrl': music.coverImgUrl,
      'play.currentTime': '00:00',
      'play.duration': '00:00',
      'play.percent': 0
    })
  },
  play: function () {
    this.audioCtx.play()
    this.setData({
      state: 'running'
    })
  },
  pause: function () {
    this.audioCtx.pause()
    this.setData({
      state: 'paused'
    })
  },
  next: function () {
    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
    this.setMusic(index)
    if (this.data.state === 'running') {
      this.play()
    }
  },
  onReady: function () {
    this.audioCtx = wx.createInnerAudioContext()
    this.setMusic(0)
    var that = this
    this.audioCtx.onError(function () {
      console.log('播放失败：' + that.audioCtx.src)
    })
    this.audioCtx.onEnded(function () {
      that.next()
    })
    this.audioCtx.onPlay(function () { })
    this.audioCtx.onTimeUpdate(function () {
      that.setData({
        'play.duration': formatTime(that.audioCtx.duration),
        'play.currentTime': formatTime(that.audioCtx.currentTime),
        'play.percent': that.audioCtx.currentTime / that.audioCtx.duration * 100
      })
    })
    function formatTime(time) {
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    }
  },
  sliderChange: function (e) {
    var second = e.detail.value * this.audioCtx.duration / 100
    this.audioCtx.seek(second)
  },
//     audioCtx:null,
//     onReady:function () {
//       this.audioCtx = wx.createInnerAudioContext()
//       this.setMusic(0)
//     },
//    setMusic: function (index) {
//     var music = this.data.playlist[index]
//     this.audioCtx.src = music.src
//     this.setData({
//       playIndex: index,
//       'play.title': music.title,
//       'play.singer': music.singer,
//       'play.coverImgUrl': music.coverImgUrl,
//       'play.currentTime': '00:00',
//       'play.duration': '00:00',
//       'play.percent': 0
//    })
//  },
//  play:function(){
//    this.audioCtx.play()
//    this.setData({
//      state:'running'
//    })
//  },
//  pause:function () {
//    this.audioCtx.pause()
//    this.setData({
//      state:'paused'
//    })
//  },
//  next:function () {
//    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 
//    0 : this.data.playIndex + 1 
//    this.setMusic(index)
//    if(this.data.state === 'running'){
//      this.play()
//    }
//  },
//  onReady:function(){
//   this.audioCtx = wx.createAudioContext()
//   this.setMusic(0)
//   var that = this
//   this.audioCtx.onError(function () {
//     console.log('播放失败:' + that.audioCtx.src)
//   })
//   this.audioCtx.onEnded(function () {
//     that.next()
//   })
//   this.audioCtx.onPlay(function(){ })
//   this.audioCtx.onTimeUpdate(function () {
//     that.setData({
//       'play.duration': formatTime(that.audioCtx.duration),
//       'play.currentTime': formatTime(that.audioCtx.currentTime),
//       'play.percent': that.audioCtx.currentTime /
//       that.audioCtx.duration * 100
//     })
//   })
//   function formatTime(time){
//     var minute = Math.floor(time / 60) % 60;
//     var second = Math.floor(time) % 60
//     return(minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
//   }
//  },
//   sliderChange: function (e) {
//     var second = e.detail.value * this.audioCtx.duration / 100
//     this.audioCtx.seek(second)
//   },

  //页面切换
  changeItem:function(e){
    this.setData({
      item:e.target.dataset.item,
    })
  },
  //tab切换
  changeTab:function(e){
    this.setData({
      tab:e.detail.current
    })
  },
  change:function(e){
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  }
  //事件处理函数
  // bindViewTap: function() {
  //   wx.navigateTo({
  //     url: '../logs/logs'
  //   })
  // },
  // onLoad: function () {
  //   if (app.globalData.userInfo) {
  //     this.setData({
  //       userInfo: app.globalData.userInfo,
  //       hasUserInfo: true
  //     })
  //   } else if (this.data.canIUse){
  //     // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
  //     // 所以此处加入 callback 以防止这种情况
  //     app.userInfoReadyCallback = res => {
  //       this.setData({
  //         userInfo: res.userInfo,
  //         hasUserInfo: true
  //       })
  //     }
  //   } else {
  //     // 在没有 open-type=getUserInfo 版本的兼容处理
  //     wx.getUserInfo({
  //       success: res => {
  //         app.globalData.userInfo = res.userInfo
  //         this.setData({
  //           userInfo: res.userInfo,
  //           hasUserInfo: true
  //         })
  //       }
  //     })
  //   }
  // },
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
})

